# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=icinga2
pkgver=2.14.2
pkgrel=1
pkgdesc="An open source host, service and network monitoring program"
url="https://icinga.com"
arch="all !armhf !armv7 !s390x"
license="GPL-2.0-only"
depends="monitoring-plugins"
makedepends="$depends_dev cmake bison flex boost-dev yajl-dev
	libpq-dev mariadb-connector-c-dev openssl-dev>3 libedit-dev samurai"
install="$pkgname.pre-install $pkgname.post-install"
pkgusers="icinga"
pkggroups="icinga icingacmd"
subpackages="$pkgname-doc $pkgname-bash-completion:bashcomp:noarch $pkgname-vim::noarch $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/Icinga/icinga2/archive/v$pkgver.tar.gz
	$pkgname.initd
	avoid-bash-in-scripts.patch
	disable-failing-tests.patch
	boost-1.81.patch
	"

# secfixes:
#   2.13.1-r0:
#     - CVE-2021-37698
#   2.11.3-r1:
#     - CVE-2020-14001

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build \
		-DCMAKE_BUILD_TYPE=None \
		-DICINGA2_GIT_VERSION_INFO=OFF \
		-DICINGA2_UNITY_BUILD=FALSE \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_SYSCONFDIR=/etc \
		-DICINGA2_RUNDIR=/run \
		-DCMAKE_INSTALL_SBINDIR=/usr/sbin \
		-DCMAKE_INSTALL_LIBDIR=/usr/lib \
		-DCMAKE_INSTALL_LOCALSTATEDIR=/var \
		-DICINGA2_SYSCONFIGFILE=/etc/icinga2 \
		-DICINGA2_PLUGINDIR=/usr/lib/monitoring-plugins \
		-DICINGA2_USER=icinga \
		-DICINGA2_GROUP=icingacmd \
		-DICINGA2_COMMAND_GROUP=icingacmd \
		-DINSTALL_SYSTEMD_SERVICE_AND_INITSCRIPT=no \
		-GNinja \
		. $CMAKE_CROSSOPTS
	ninja -C build
}

check() {
	ninja -C build test
}

package() {
	cd "$builddir"/build
	DESTDIR="$pkgdir" ninja install
	mv "$pkgdir/etc/icinga2/conf.d" "$pkgdir/etc/icinga2/conf.d.example"
	mkdir "$pkgdir/etc/icinga2/conf.d"
	rm "$pkgdir/etc/icinga2/features-enabled/checker.conf"
	rm "$pkgdir/etc/icinga2/features-enabled/mainlog.conf"
	rm "$pkgdir/etc/icinga2/features-enabled/notification.conf"
	rm -r "$pkgdir/run"

	mkdir -p "$pkgdir/usr/lib/tmpfiles.d"
	cat > "$pkgdir/usr/lib/tmpfiles.d/icinga2.conf" <<- EOF
d /run/icinga2 0750 icinga icingacmd -
d /run/icinga2/cmd 2750 icinga icingacmd -
EOF
	cd ..

	install -Dm644 tools/syntax/vim/ftdetect/icinga2.vim "$pkgdir/usr/share/vim/vimfiles/ftdetect/icinga2.vim"
	install -Dm644 tools/syntax/vim/syntax/icinga2.vim "$pkgdir/usr/share/vim/vimfiles/syntax/icinga2.vim"
	install -Dm644 tools/syntax/nano/icinga2.nanorc "$pkgdir/usr/share/nano/icinga2.nanorc"

	chmod 750 "$pkgdir/etc/icinga2" \
		"$pkgdir/var/lib/icinga2" \
		"$pkgdir/var/spool/icinga2" \
		"$pkgdir/var/cache/icinga2" \
		"$pkgdir/var/log/icinga2"
	mkdir -p "$pkgdir/run/icinga2"
	chown -R icinga:icinga "$pkgdir"/etc/icinga2
	chown -R icinga:icinga "$pkgdir"/var/lib/icinga2
	chown icinga:icinga "$pkgdir"/var/spool/icinga2
	chown -R icinga:icingacmd "$pkgdir"/var/cache/icinga2
	chown -R icinga:icingacmd "$pkgdir"/var/log/icinga2
	chown -R icinga:icingacmd "$pkgdir"/run/icinga2

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	cat >"$pkgdir"/usr/share/doc/$pkgname/README.alpine <<EOF

Add the web server user to the icingacmd group in order to grant it write
permissions to the external command pipe and livestatus socket:

For nginx, as example:
# adduser nginx icingacmd

For Apache:
# adduser apache icingacmd

For lighttpd:
# adduser lighttpd icingacmd
EOF
}

bashcomp() {
	depends="bash"
	pkgdesc="Bash completions for $pkgname"

	mkdir -p "$subpkgdir"/usr/share/bash-completion/completions
	mv "$pkgdir"/etc/bash_completion.d/$pkgname "$subpkgdir"/usr/share/bash-completion/completions/$pkgname
}

vim() {
	pkgdesc="$pkgdesc (vim syntax)"
	depends=

	mkdir -p "$subpkgdir"/usr/share/vim
	cp -r "$builddir"/tools/syntax/vim "$subpkgdir"/usr/share/vim/vimfiles
}

sha512sums="
a7574254eed715c32317feaac57d5444a9d2bcccbfe06adbcee4d7d3c2928ecb1f381a5321c219406816bb39120eb9e95ca073399844cbafde274d289f619f57  icinga2-2.14.2.tar.gz
46890756104563b1a4bc60b5952a2d63f5263883cfe702de7ccc4616dfe5b20d380ff1bc190482741a06bcfe7e662ad14a6965e26b9ce916ef56a1248d6b524c  icinga2.initd
523f93ab661e43915241bce70fbecfd6834830a38f5b3dce6f2c856712e2e5a58fe1b5f970d5f23295a1c5e0d811f7e3a5b54acdd74e31112b309e31a9dc05a6  avoid-bash-in-scripts.patch
7e72b483ab234efcb81dad8aabd37491210f13cf0f7e9b27ee381adb227301a92faf88100bf1c70e7e38c594a607bf210cda007f6d397b2e14fd02442f885091  disable-failing-tests.patch
81ac38b64b49abf78f855ad7558afa1115175f7e84b20d407c402848bbd8484dcd76a3f4547fafc6b083b5e69927ee60a407a530e0fbd78a47ff73150a1a5fe8  boost-1.81.patch
"
